import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

import LocalCache from "@/utils/cache"
import { firstMenu } from "@/utils/map-menu"

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/main'
  },
  {
    path: '/main',
    name: 'main',
    component: () => import('@/views/main/main.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue')
  },
  {
    path: "/:pathMatch(.*)*",
    name: 'not-found',
    component: () => import("@/views/not-found/not-found.vue")
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由拦截
router.beforeEach(to => {
  if (to.path !== '/login') {
    const token = LocalCache.getCache("token")
    if (!token) return '/login'
  }
  if (to.path === '/main') {
    return firstMenu.url
  }

})
export default router
